<style lang="less" scoped>
.main-keyword-group {
    .btn-wrap {
        padding: 5px 0;
    }
    .online,
    .offline {
        margin-left: 0;
    }
    .second-row .manage-templet {
        margin-top: 5px;
    }
    .group {
        display: inline-block;
        vertical-align: top;
    }
}
</style>
<template>
    <div class="main main-keyword-group">
        <div class="main-header">
            <el-breadcrumb>
                <el-breadcrumb-item :to="{ path: '/tickets' }">票券管理</el-breadcrumb-item>
                <el-breadcrumb-item>票券管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="main-filters">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-radio-group v-model="query.deleted">
                        <el-radio-button label="all">全部</el-radio-button>
                        <el-radio-button label="0">未删除</el-radio-button>
                        <el-radio-button label="1">已删除</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item>
                    <el-radio-group v-model="query.status">
                        <el-radio-button label="all">全部</el-radio-button>
                        <el-radio-button
                            v-for="(item, id) in statusAlias"
                            :label="id"
                        >{{item.name}}</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="票券大类">
                    <el-select v-model="query.parent_id">
                        <el-option label="全部" value="all" key=""></el-option>
                        <el-option
                            v-for="item in grands"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="票券类型">
                    <el-select v-model="query.type_id">
                        <el-option label="全部" value="all" key=""></el-option>
                        <el-option
                            v-for="item in parents"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="有效截止时间">
                    <el-date-picker type="date" v-model="query.end_time"></el-date-picker>
                </el-form-item>
                <el-form-item label="入库日期">
                    <el-date-picker type="date" v-model="query.end_time"></el-date-picker>
                </el-form-item>
                <el-form-item label="客户搜索">
                    <el-select v-model="query.customer_id">
                        <el-option label="全部" value="all" key=""></el-option>
                        <el-option
                            v-for="item in customers"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核单号">
                    <el-input v-model="query.apply_no"></el-input>
                </el-form-item>
                <el-form-item label="票券编号">
                    <el-input v-model="query.ticket_no"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div class="main-actions">
            <div class="main-actions-label">
                共<em>{{itemsData.pagination.total}}</em>条
            </div>
            <!-- <div class="main-actions-btns" v-if="user.role.name.indexOf('超级管理员') > -1">
                <el-button @click="pullItems">批量延期</el-button>
            </div> -->
            <div class="main-actions-btns">
                <el-button @click="pullItems">批量删除</el-button>
            </div>
        </div>
        <el-table
            border
            element-loading-text="Loading..."
            v-loading.body="!itemsData.$resolved"
            :data="itemsData.items"
            @sort-change="onSortItems"
            @selection-change="onSelect"
        >
            <el-table-column sortable="custom" prop="id" label="ID" width="80"></el-table-column>
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column width="150" prop="ticket_no" label="票券编号"></el-table-column>
            <el-table-column width="80" prop="price" label="价格"></el-table-column>
            <!-- <el-table-column width="100" prop="private_price" label="协议价"></el-table-column> -->
            <!-- <el-table-column width="100"  label="票券大类" inline-template>
                <div>
                    <span>{{ row.grandpa ? row.grandpa.name : '-' }}</span>
                </div>
            </el-table-column> -->
            <el-table-column width="200" label="票券类型" inline-template>
                <div>
                    <span>{{ row.parent ? row.parent.name : '-'}}-{{row.types.name}}-{{row.type}}</span>
                </div>
            </el-table-column>
            <el-table-column label="到期时间" width="200" inline-template>
                <div>
                    <span>{{ row.end_time | date }}</span>
                </div>
            </el-table-column>
            <el-table-column label="生成时间" inline-template>
                <div>
                    <span>{{ row.created_at | date}}</span>
                </div>
            </el-table-column>
            <el-table-column prop="status" label="状态" inline-template>
                <div>
                    <el-tag v-if="row.deleted" type="danger">已删除</el-tag>
                    <el-tag v-if="!row.deleted" :type="statusAlias[row.status].icon">{{statusAlias[row.status].name}}</el-tag>
                </div>
            </el-table-column>
            <!-- <el-table-column label="状态属性" width="100" inline-template>
                <div>
                    <el-tag type="success" v-if="row.status === 1">已上架</el-tag>
                    <el-tag type="warning" v-else>已下架</el-tag>
                </div>
            </el-table-column> -->
            <!-- <el-table-column sortable="custom" prop="priority" label="排序"></el-table-column> -->
            <el-table-column label="操作" inline-template>
                <div class="item-menus">
                     <router-link :to="'/tickets/modify/'+row.id"  v-if="!row.deleted && user.role.name.indexOf('超级管理员') > -1"><el-button size="small">编辑</el-button></router-link>
                    <el-button size="small" type="danger" v-if="!row.deleted && user.role.name.indexOf('超级管理员') > -1" @click="deleteItem(row)">删除</el-button>
                    <!-- <el-button size="small" type="success" v-if="row.grand_id === 0" @click="editItem(row, 2)">新增子票券</el-button> -->
                </div>
            </el-table-column>
        </el-table>

        <el-pagination
            v-if="itemsData.$resolved"
            layout="prev, pager, next, jumper"
            :total="itemsData.pagination.total"
            :current-page="query.page_num"
            :page-size="query.page_size"
            @current-change="onPageChange"
        >
        </el-pagination>

        <el-dialog
            :title="editData.title"
            :visible.sync="editDialogVisible"
            size="tiny"
        >
            <el-form :model="editData">
                <el-form-item label="票券名称">
                    <el-input v-model="editData.name" placeholder="输入票券名"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitEdit">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import ListController from '../controllers/list';

import TicketModel from '../models/ticket';
import TypeModel from '../models/type';
import CustomModel from '../models/customer';
import Promise from 'bluebird';
import TTXS from '../common/ttxs';

export default new ListController({
    data() {
        return {
            ListModel: TicketModel,
            user: TTXS.user,
            query: {
                page_num: 1,
                page_size: 20,
                parent_id: 'all',
                type_id: 'all',
                status: 'all',
                deleted: 0,
                sort: '',
                end_time: '',
                apply_no: '',
                ticket_no: '',
                customer_id: 'all'
            },

            statusMap: {
                PENDING: TicketModel.PENDING,
                USED: TicketModel.USED,
                OFFLINE: TicketModel.OFFLINE,
                UNUSED: TicketModel.UNUSED,
                COMPLETED: TicketModel.COMPLETED
            },
            statusAlias: {
                [TicketModel.PENDING]: {
                    icon: 'warning',
                    name: '待审核'
                },
                [TicketModel.UNUSED]: {
                    icon: 'success',
                    name: '未使用'
                },
                [TicketModel.OFFLINE]: {
                    icon: 'gray',
                    name: '未出票'
                },
                [TicketModel.READY]: {
                    icon: 'gray',
                    name: '备售'
                },
                [TicketModel.USED]: {
                    icon: 'success',
                    name: '已使用未核销'
                },
                [TicketModel.COMPLETED]: {
                    icon: 'success',
                    name: '已核销'
                }
            },

            parents: [],
            grands: [],
            customers: [{
                id: 1,
                name: '中国银行'
            }, {
                id: 1,
                name: '农业银行'
            }],

            editData: {},
            editDialogVisible: false
        };
    },
    methods: {
        beforeLoadItems() {
            return Promise.all([
                this.loadParents(),
                this.loadGrands(),
                this.loadCustomers()
            ]);
        },
        loadGrands() {
            return TypeModel.query({
                parent_id: 0,
                grand_id: 0
            })
            .$promise
            .then(types => {
                this.grands = types.items;
            });
        },
        loadParents(parent_id) {
            return TypeModel.query({
                parent_id: parent_id
            })
            .$promise
            .then(types => {
                this.parents = types.items;
            });
        },
        loadCustomers() {
            return CustomModel.query({
            })
            .$promise
            .then(types => {
                this.customers = types.items;
            });
        },
        debugItem(row) {
            this.$confirm('确认调试该项？')
            .then(() => {
                this.ListModel.update({
                    id: row.id,
                    status: 1
                }).$promise;
            })
            .catch(err => {
                throw new Error(err);
            });
        },
        pullItem(row) {
            this.$confirm('确认下线该项？')
            .then(() => {
                const params = Object.assign({}, row, {
                    id: row.id,
                    status: 0
                });
                this.ListModel.update(params).$promise;
            })
            .then(() => {
                this.$message({
                    message: '下线成功！',
                    type: 'success'
                });

                this.loadItems();
            })
            .catch(err => {
                this.$message.error(err);
            });
        },
        pushItem(row) {
            this.$confirm('确认上线该项？')
            .then(() => {
                return this.ListModel.update({
                    id: row.id,
                    status: 1
                }).$promise
                .then(() => {
                    this.loadItems();
                });
            })
            .catch(err => {
                throw new Error(err);
            });
        },
        pullItems() {
            return this.setItems(this.selectedItems, {
                deleted: 1
            }, {
                confirm: '确认删除选中项？'
            });
        },
        deleteItem(row) {
            this.$confirm('确认删除该票券？')
            .then(() => {
                return this.ListModel.delete({
                    id: row.id
                }).$promise;
            })
            .then(() => {
                return this.loadItems();
            })
            .catch(err => {
                throw new Error(err);
            });
        },
        editItem(row, type) {
            this.editDialogVisible = true;
            if(type === 1 || !type) {
                this.editData = Object.assign({}, {name: ''}, row);
                return;
            }
            if(row && row.parent_id === 0) {
                this.editData = Object.assign({}, {name: '', parent_id: row.id });
                return;
            }

            if(row && row.grand_id === 0) {
                this.editData = Object.assign({}, {name: '', parent_id: row.id, grand_id: row.parent_id });
            }           
        },
        submitEdit() {
            let params = null;
            let $submitPromise = null;

            if(this.editData.id > 0) {
                $submitPromise = this.ListModel.update(this.editData).$promise;
            }
            else {
                params = Object.assign({}, this.editData);
                $submitPromise = this.ListModel.save(params).$promise;
            }

            $submitPromise
            .then(res => {
                if(res.code && res.code !== 200) {
                    this.$message.error(res.message);
                    return;
                }
                this.editDialogVisible = false;
                this.$message({
                    message: '提交成功！',
                    type: 'success'
                });
                this.loadItems();
            })
            .catch(err => {
                this.$message.error(err);
            });
        }
    }
});
</script>
